﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.MetaData.Builders;
using SchoolPointe.RelatedContent.Enums;
using SchoolPointe.RelatedContent.Models;

namespace SchoolPointe.RelatedContent.Extensions
{
    public static class ContentTypePartDefinitionBuilderExtensions
    {
        public static ContentTypePartDefinitionBuilder WithContentRelationship(this ContentTypePartDefinitionBuilder builder, string relationshipName, string relationshipIdentifier, RelationType relationshipType, bool isRequired, string[] contentTypeLimitations, string editorView = null, string inverseOf = null)
        {
            var relationship = new RelatedContentRelationship
                                   {
                                       Name = relationshipName,
                                       ContentTypeLimitations =
                                           contentTypeLimitations == null || contentTypeLimitations.Length == 0 || contentTypeLimitations.Contains("*")
                                               ? "*"
                                               : string.Join(",", contentTypeLimitations),
                                       Identifier = string.IsNullOrEmpty(relationshipIdentifier) ? relationshipName.Replace(" ", "-") : relationshipIdentifier,
                                       IsRequired = isRequired,
                                       RelationType = relationshipType,
                                       EditorView = editorView,
                                       InverseOf = inverseOf
                                   };
            var key = "RelatedContentPartSettings." + relationship.Identifier;
            builder.WithSetting(key, relationship.ToJson());
            return builder;
        }
    }
}